package com.example.demo.service.impl;
import com.example.demo.dao.SysUserDao;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


/**
 * @author Administrator
 * @className wuyp
 * @description:
 * @Date 2022/3/25 8:57
 **/
@Service
@Transactional(rollbackOn = Exception.class )
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Override
    public SysUser findByUsername(String userName) {
        return sysUserDao.findByUsername(userName);
    }

    @Override
    public SysUser getUser(String username, String password) {
        return sysUserDao.getUser(username,password);
    }

    @Override
    public List<SysUser> findAll() {
        return sysUserDao.findAll();
    }
    @Override
    public SysUser addUser(SysUser user) {
        return sysUserDao.save(user);
    }

    @Override
    public void deleteUserById(Integer id) {
           sysUserDao.deleteById(id);
    }

    @Override
    public void deleteBatchUsers(List<Integer> ids) {
        sysUserDao.deleteAllByIdInBatch(ids);
    }

    @Override
    public SysUser findById(Integer id) {
        SysUser user =new SysUser();
        user.setId(id);
        Example<SysUser> example= Example.of(user);
        return sysUserDao.findAll(example).get(0);
    }

    @Override
    public void updateUserById(SysUser user) {
        sysUserDao.saveAndFlush(user);
    }
}
